/*
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.gateway.bean;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.hyts.infodb.bean.bo.BaseBO;
import com.hyts.infodb.bean.bo.CondtionBO;
import com.hyts.infodb.gateway.config.GatewayConfig;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;

/**
 * @author LiBo/Alex
 * @see com.hyts.infodb.bean.bo.BaseBO
 * @since V1.0
 * @version V1.0
 */
@Data
@Slf4j
@Accessors(chain=true)
public class RouteDefinitionBO extends BaseBO{

    /**
     * @fieldName serialVersionUID
     * @fieldType long
     */
    private static final long serialVersionUID = 437969048892224112L;

    /**
     * @fieldName name
     * @fieldType String
     */
    private String name;

    /**
     * 用作处理注册中心的应用名称使用
     */
    private String serviceId;

    /**
     * <p>映射转发的url路径</p>
     * @fieldName url
     * @fieldType String
     */
    private String url;

    /**
     * <p>令牌桶流速</p>
     * @fieldName limitRate
     * @fieldType Integer
     */
    private Integer limitRate;

    /**
     * <p>令牌桶容量</p>
     * @fieldName limitCapacity
     * @fieldType Integer
     */
    private Integer limitCapacity;

    /**
     * <p>状态</p>
     * @fieldName status
     * @fieldType Byte
     */
    private Byte status;


    /**
     * <p>路由顺序编号优先级</p>
     * @fieldName routeOrder
     * @fieldType Integer
     */
    private Integer routeOrder;


    /**
     * @fieldName predicates
     * @fieldType String
     */
    private String predicates;


    /**
     * @fieldName filters
     * @fieldType String
     */
    private String filters;


    private String path;


    /**
     * @param override
     * @return
     * @exception
     */
    public RouteDefinitionBO buildDefaultPredicates(boolean override,String path) {
        if(override || StringUtils.isEmpty(this.predicates)) {
            List<PredicateDefinition> dataObj = new LinkedList<>();
            PredicateDefinition pd = new PredicateDefinition();
            Map<String,String> dataStr = new HashMap<String,String>() {
                /**
                 * @fieldName serialVersionUID
                 * @fieldType long
                 */
                private static final long serialVersionUID = 1L;
                {
                    pd.setName(GatewayConfig.DEFAULT_PREDICATE_KEY_PATH);
                    put(GatewayConfig.DEFAULT_PREDICATE_VALUE_PATH,path);
                }
            };
            pd.setArgs(dataStr);
            dataObj.add(pd);
            String predicatesJsonStr = JSONObject.toJSONString(dataObj);
            log.info(predicatesJsonStr);
            this.setPredicates(predicatesJsonStr);
        }
        return this;
    }

    /**
     * @param override
     * @return
     * @exception
     */
    public RouteDefinitionBO buildDefaultFilters(boolean override,Integer stripFix) {
        if(override || StringUtils.isEmpty(this.filters)) {
            List<FilterDefinition> dataObj = new LinkedList<>();
            FilterDefinition pd = new FilterDefinition();
            pd.setArgs(new HashMap<String,String>() {
                /**
                 * @fieldName serialVersionUID
                 * @fieldType long
                 */
                private static final long serialVersionUID = 1L;
                {
                    pd.setName(GatewayConfig.DEFAULT_FILTER_STRIP_KEY);
                    put(GatewayConfig.DEFAULT_FILTER_STRIP_VALUE,stripFix.toString());
                }
            });
            dataObj.add(pd);
            String filterJsonStr = JSONObject.toJSONString(dataObj);
            log.info(filterJsonStr);
            this.setFilters(filterJsonStr);
        }
        return this;
    }

}
